#include <iostream>
#include <sys/epoll.h>
#include <cstring>
#include "log.hpp"

namespace EpollIn
{
    class Epoll
    {
    const int size = 1;//针对epoll_create的参数,其在linux2.6.8后废弃,但需大于一
    public:
        Epoll()
        {
            _epfd = epoll_create(size);
            if(_epfd == -1)
            {
                mylog::log(Fatal,"epoll_create获取句柄失败,errno:%d,strerror:%s",errno,strerror(errno));
                exit(errno);
            }
        }

        ~Epoll()
        {
            if(_epfd >=0) close(_epfd);
        }

        void EpollCtl(int op,int fd,struct epoll_event& event)
        {
            int n = epoll_ctl(_epfd,op,fd,&event);
            if(n == -1)
            {
                mylog::log(Warning,"epoll_ctl设置文件描述符失败,errno:%d,strerror:%s",errno,strerror(errno));
                close(fd);
            }
        }

        int EpollWait(struct epoll_event *events,int maxevents,int timeout)
        {
            int n = epoll_wait(_epfd,events,maxevents,timeout);
            return n;
        }



    private:
        int _epfd;

    };
}